package com.xiaoqi.reggie.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoqi.reggie.common.CustomException;
import com.xiaoqi.reggie.entity.Category;
import com.xiaoqi.reggie.entity.Dish;
import com.xiaoqi.reggie.entity.Setmeal;
import com.xiaoqi.reggie.mapper.CategoryMapper;
import com.xiaoqi.reggie.service.CategoryService;
import com.xiaoqi.reggie.service.DishService;
import com.xiaoqi.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    SetmealService setmealService;
    @Autowired
    DishService dishService;
    /**
     * 根据id删除，并且查询有无与之相关的菜品和套餐，若有则抛出自定义异常
     * @param id
     * @return
     */
    @Override
    public void remove(Long id) {
        //查询菜品
        LambdaQueryWrapper<Dish> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = dishService.count(queryWrapper);
        if (count>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal>lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lambdaQueryWrapper);
        if (count1>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        super.removeById(id);
    }
}
